<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <el-col :span="20">
          <el-input
            v-model="searchModel.releaseDonateName"
            placeholder="物品名称"
            clearable
          />
          <el-input
            v-model="searchModel.releaseDonateType"
            placeholder="物品类型"
            clearable
          />
          <el-button
            type="primary"
            round
            icon="el-icon-search"
            @click="getReleaseDonateList"
          >查询</el-button>
        </el-col>
        <el-col :span="4" align="right">
          <el-button
            type="primary"
            icon="el-icon-plus"
            circle
            @click="openEditUI(null)"
          />
        </el-col>
      </el-row>
    </el-card>

    <!-- 结果列表 -->
    <el-card>
      <el-table :data="releaseDonateList" stripe style="width: 100%">
        <el-table-column label="#" width="80">
          <template slot-scope="scope">
            <!-- (pageNo-1) * pageSize + index + 1 -->
            {{
              (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1
            }}
          </template>
        </el-table-column>
        <el-table-column prop="id" label="用户ID" width="100" />
        <el-table-column prop="releaseDonateName" label="捐赠物品名称" width="150" />
        <el-table-column prop="releaseDonateIntroduce" label="捐赠物品介绍" width="150" />
        <el-table-column prop="releaseDonateStatus" label="物品状态" width="150">
<!--          <template slot-scope="scope">-->
<!--            <el-tag v-if="scope.row.activityStatus == 1 ">进行中</el-tag>-->
<!--            <el-tag v-else-if="scope.row.activityStatus == 2" type="danger">已过期</el-tag>-->
<!--            <el-tag v-else-if="scope.row.activityStatus == 0" >未开启</el-tag>-->
<!--          </template>-->
        </el-table-column>
        <el-table-column prop="releaseDonateAddress" label="捐赠物品的地点" width="150" />
        <el-table-column prop="releaseDonateType" label="捐赠物品的类型" width="150" />
        <el-table-column prop="releaseDonateUseYears" label="使用年限(几年以内)" width="150" />
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" circle @click="openEditUI(scope.row.id)" />
            <el-button type="danger" icon="el-icon-delete" size="mini" circle @click="deleteDonate(scope.row)" />
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分页组件 -->
    <el-pagination
      :current-page="searchModel.pageNo"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="searchModel.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />

    <!-- 用户信息编辑对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      @close="clearForm"
    >
      <el-form ref="activityFormRef" :model="releaseDonateForm" :rules="rules">
        <el-form-item
          label="物品的名称"
          prop="releaseDonateName"
          :label-width="formLabelWidth"
        >
          <el-input v-model="releaseDonateForm.releaseDonateName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="物品的介绍" :label-width="formLabelWidth">
          <el-input v-model="releaseDonateForm.releaseDonateIntroduce" autocomplete="off" />
        </el-form-item>
        <el-form-item label="物品的地点" :label-width="formLabelWidth">
          <el-input v-model="releaseDonateForm.releaseDonateAddress" autocomplete="off" />
        </el-form-item>
        <el-form-item label="物品的状态" :label-width="formLabelWidth">
          <el-input v-model="releaseDonateForm.releaseDonateStatus" autocomplete="off" />
        </el-form-item>
        <el-form-item label="物品的类型" :label-width="formLabelWidth">
          <el-input v-model="releaseDonateForm.releaseDonateType" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="物品使用年限" :label-width="formLabelWidth">
          <el-input v-model="releaseDonateForm.releaseDonateUseYears" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveActivity">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import releaseDonateApi from '@/api/releaseDonate'
export default {
  data() {
    return {
      formLabelWidth: '100px',
      releaseDonateForm: {
        releaseDonateName: '',
        releaseDonateIntroduce: '',
        releaseDonateType: '',
        releaseDonateUseYears:'',
        releaseDonateAddress: '',
        releaseDonateStatus: ''
      },
      dialogFormVisible: false,
      title: '',
      total: 0,
      searchModel: {
        pageNo: 1,
        pageSize: 10,
        activityStatus: ''
      },
      releaseDonateList: [],
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 50,
            message: '长度在 3 到 50 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.getReleaseDonateList()
  },
  methods: {
    deleteDonate(donate) {
      this.$confirm(`您确认删除物品 ${donate.releaseDonateName} ?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        releaseDonateApi.deleteDonateById(donate.id).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          })
          this.getReleaseDonateList()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    saveActivity() {
      // 触发表单验证
      this.$refs.activityFormRef.validate((valid) => {
        if (valid) {
          // 提交请求给后台
          releaseDonateApi.saveDonate(this.releaseDonateForm).then(response => {
            // 成功提示
            this.$message({
              message: response.message,
              type: 'success'
            })
            // 关闭对话框
            this.dialogFormVisible = false
            // 刷新表格
            this.getReleaseDonateList()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    clearForm() {
      this.releaseDonateForm = {}
      this.$refs.activityFormRef.clearValidate()
    },
    openEditUI(id) {
      if (id == null) {
        this.title = '新增物品'
      } else {
        this.title = '修改物品'
        // 根据id查询用户数据
        releaseDonateApi.getDonateById(id).then(response => {
          this.releaseDonateForm = response.data
        })
      }
      this.dialogFormVisible = true
    },
    handleSizeChange(pageSize) {
      this.searchModel.pageSize = pageSize
      this.getReleaseDonateList()
    },
    handleCurrentChange(pageNo) {
      this.searchModel.pageNo = pageNo
      this.getReleaseDonateList()
    },
    getReleaseDonateList() {
      releaseDonateApi.getDonateList(this.searchModel).then((response) => {
        this.releaseDonateList = response.data.rows
        this.total = response.data.total
      })
    }
  }
}
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}
.el-dialog .el-input {
  width: 85%;
}
</style>
